package com.examples;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;

import org.apache.commons.io.IOUtils;

public class Loader {

	// private static final String baseIssuePattern =
	// "<a href=\"/tracker/\\?func=detail&aid=(.+?)&group_id=:groupId&atid=:atid\">(.+?)</a>";

	private static final String baseIssuePattern = "<tr> <td class=\"p.\"> (.+?) </td> <td> (.+?) </td> <td> <a href=\"/tracker/\\?group_id=:groupId&atid=(.+?)\">(.+?)</a> </td> <td class=\"summary\"> <a href=\"/tracker/index.php\\?func=detail&aid=(.+?)&group_id=:groupId&atid=(.+?)\">(.+?)</a> </td> <td> (.+?) </td> <td> (.+?) </td> <td> (.+?);(.+?);(.+?) </td> <td> (.+?) </td>";

	public static void main(String[] args) throws MalformedURLException, IOException {
		// Authenticator.setDefault(new SimpleAuthenticator("", ""));

		String groupId = "44253";
		int offset = 25;

		String url = "http://sourceforge.net/search/index.php?group_id=:groupId&offset=:offset&type_of_search=artifact&q=&artifact_id=&submitted_by=&assigned_to=&open_date_start=&open_date_end=&last_update_date_start=&last_update_date_end=&form_submit=Search";

		url = url.replace(":groupId", groupId);

		String issueStrPattern = baseIssuePattern.replace(":groupId", groupId);

		Pattern issuePattern = Pattern.compile(issueStrPattern);
		Pattern patternDev = Pattern.compile("<a href=\"/users/(.+?)/");

		for (int i = 0; i < 25; i += offset) {
			String urlToFetch = url.replace(":offset", String.valueOf(i));
			// System.out.println("Analyzing url: " + urlToFetch);
			InputStream inputStream = new URL(urlToFetch).openStream();
			String cleanedPage = readPage(inputStream);

			Matcher matcherIssue = issuePattern.matcher(cleanedPage);
			int num = 1;
			while (matcherIssue.find()) {
				String[] issue = new String[matcherIssue.groupCount()];
				for (int j = 1; j <= matcherIssue.groupCount(); j++) {
					issue[j - 1] = matcherIssue.group(j);
				}
				System.out.println();
				System.out.println("***************************************");
				System.out.println(num);
				System.out.println(Arrays.toString(issue));
				System.out.println("priority: " + issue[0]);
				System.out.println("id:" + issue[1]);
				System.out.println("type:" + issue[3]);
				System.out.println("description:" + issue[6]);

				String developer = issue[7];
				Matcher matcherDev = patternDev.matcher(issue[7]);
				if (matcherDev.find()) {
					developer = matcherDev.group(1);
				}
				System.out.println("developer:" + developer);
				System.out.println("status:" + issue[11]);
				System.out.println("date:" + issue[12]);
				num++;
			}
		}

	}

	@SuppressWarnings("unchecked")
	private static String readPage(InputStream inputStream) throws IOException {
		List<String> readLines = IOUtils.readLines(inputStream);
		StringBuilder builder = new StringBuilder();
		for (String line : readLines) {
			builder.append(line);
		}
		String cleanedLines = builder.toString().replaceAll("\n", "").replaceAll("\\s+", " ");
		return cleanedLines;
	}

}
